package pl.jtb.ctl.tabory.dao.impl.mybatis.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

import pl.jtb.ctl.tabory.dao.api.services.IVagonService;
import pl.jtb.ctl.tabory.dao.data.vo.VagonVO;
import pl.jtb.ctl.tabory.dao.impl.mybatis.base.BaseDaoService;

public class VagonServiceImpl extends BaseDaoService<VagonVO> implements IVagonService {

    private final Logger logger = Logger.getLogger(VagonServiceImpl.class);

    private final static String MAPPER = "pl.jtb.ctl.taboty.VagonMapper";
    private final static String SQL_SELECT_ALL = ".selectAllVagons";
    private final static String SQL_INSERT = ".insertVagon";
    private final static String SQL_UPDATE = ".updateVagon";
    private final static String SQL_DELETE = ".deleteVagon";
    private final static String SQL_SELECT_FOR_LIST = ".selectVagonsForListId";
    private final static String SQL_SELECT_BY_ID = ".selectVagonById";
    private final static String SQL_SELECT_BY_INTERNAL_ID = ".selectVagonByInternalId";

    public VagonServiceImpl(SqlSessionFactory sqlSessionFactory) {
        super(sqlSessionFactory);
    }

    @Override
    public List<VagonVO> getListAllVagons() throws Exception {
        logger.debug("Getting all Vagons");
        List<VagonVO> result = null;
        try {
            result = selectList(MAPPER + SQL_SELECT_ALL, null);
        } catch (Exception e) {
            result = new ArrayList<VagonVO>();
            logger.error(e.getMessage(), e);
            throw e;
        }
        return result;
    }

    @Override
    public VagonVO getVagon(final Long vagonId) throws Exception {
        logger.debug("Getting vagon by id:" + vagonId);
        VagonVO result = null;
        try {
            result = selectOne(MAPPER + SQL_SELECT_BY_ID, vagonId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
        return result;
    }

    @Override
    public VagonVO saveUpdateVagon(final VagonVO vagon) throws Exception {
        logger.debug("Saving/Updating Vagon: " + vagon);
        vagon.setActive(true);
        if (vagon.getId() == null) {
            saveVagon(vagon);
        } else {
            updateVagon(vagon);
        }
        return vagon;
    }

    private void updateVagon(final VagonVO vagon) throws Exception {
        logger.debug("Updating existing Vagon");
        try {
            update(MAPPER + SQL_UPDATE, vagon);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    private void saveVagon(final VagonVO vagon) throws Exception {
        logger.debug("Saving new Vagon");
        try {
            create(MAPPER + SQL_INSERT, vagon);
        } catch (Exception e) {
            vagon.setId(null);
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public void deleteVagon(VagonVO vagon) throws Exception {
        try {
            delete(MAPPER + SQL_DELETE, vagon.getId());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public List<VagonVO> getVagons(List<Long> listVagonId) throws Exception {
        logger.debug("Getting vagons for list id: " + listVagonId);
        List<VagonVO> resul = null;
        try {
            resul = selectList(MAPPER + SQL_SELECT_FOR_LIST, listVagonId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
        return resul;
    }

    @Override
    public VagonVO getVagon(String internalId) throws Exception {
        logger.debug("Getting vagon for internal id: " + internalId);
        VagonVO result = null;
        try {
            result = selectOne(MAPPER + SQL_SELECT_BY_INTERNAL_ID, internalId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
        return result;
    }

}
